<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>博客登陆页</title>

    <link rel="stylesheet" href="css/common.css">
    <link rel="stylesheet" href="css/login.css">

</head>

<body>
    <div class="nav">
        <img src="pic/picture.jpg" alt="">      <!--左上角头像-->
        <span class="nav-span" href="blog_personal.html">BOKEE</span>
        <div class="space"></div>
        <a class="nav-span" href="blog_list.html">主页</a>
        <a class="nav-span" href="blog_register.html">注册</a>
        <a class="nav-span" href="">个人中心</a>
    </div>

    <div class="container-login">
        <div class="login-dialog">
            <h3>登陆</h3>
            <div class="row">
                <span>用户名:</span>
                <input type="text" name="username" id="username">
            </div>
            <div class="row">
                <span>密&nbsp;&nbsp;&nbsp;码:</span>
                <input type="password" name="password" id="password">
            </div>
            <!--验证码-->
            <div class="captcha">
                <span>验证码:</span>
                <input type="text" name="inputCaptcha" id="inputCaptcha">
            <!--src后端接口的地址，yml文件中-->
                <img id="checknum" src="/admin/check" style="cursor: pointer;" onclick="getcode()" title="看不清？换一张">
            </div>

            <div class="row">
                <button id="submit" onclick="login()" style="cursor: pointer;">提交</button>

            </div>
            <div class="row">
                <a href="">忘记密码</a>
            </div>

        </div>
    </div>
    <script src="js/jquery.min.js"></script>
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.4/jquery.min.js"></script>
    <script>
        // 点击事件
        $("#checknum").click(function(){
             $(this).hide().attr('src', '/admin/check?dt=' + new Date().getTime()).fadeIn();
            //  dt表示重新获取请求
        });



        
        function login() {
            // location.assign("blog_list.html");
            $.ajax({
                type:"post",
                url:"/user/login",
                data:{
                    userName:$("#username").val(),
                    password:$("#password").val(),
                    captcha:$("#inputCaptcha").val()
                },
                success:function(result){
                    if(result.code==200 && result.data!=null){
                        //密码正确
                        //存储token
                       
                        localStorage.setItem("user_token",result.data);
                        location.href="blog_list.html";
                    }else{
                        //加上自动刷新
                        $("#checknum").click(function(){
                            $(this).hide().attr('src', '/admin/check?dt=' + new Date().getTime()).fadeIn();
                            //  dt表示重新获取请求
                        });
                        alert(result.errorMsg);
                    }
                }
            });
        }
    
        
        //验证码
        function getcode(){
            jQuery.ajax({
                url:'/admin/check',
                type:'get',
                data:{
                    captcha:$("#inputCaptcha").val()
                },
                success:function(result){
                    if(result.code == 200 && result.data != null){
                        jQuery("#checknum").attr("src",result.data);
                    }else{
                        
                    }
                }
            });
        }
    
    </script>
</body>

